package bb.lanxing.fragment.sport;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings.DevicePanelSettingActivity;

import bb.lanxing.R;
import bb.lanxing.adapter.sport.DevicePanelSettingTypeAdapter;
import bb.lanxing.fragment.base.BaseFragment;

public class DevicePanelSettingTypeFragment extends BaseFragment {
    public int mSelectedTab = -1;
    private RecyclerView mRecyclerView;
    private DevicePanelSettingTypeAdapter mDevicePanelSettingTypeAdapter;
    private int mBigType;
    private int[] typeList;

    public DevicePanelSettingTypeFragment(int bigType, int[] types) {
        mBigType = bigType;
        typeList = types;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sport_record_list, container, false);

        mRecyclerView = view.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        getDevicePanelSettingTypeAdapter();
        mRecyclerView.setAdapter(mDevicePanelSettingTypeAdapter);
        return view;
    }

    public DevicePanelSettingTypeAdapter getDevicePanelSettingTypeAdapter() {
        if (mDevicePanelSettingTypeAdapter == null) {
            mDevicePanelSettingTypeAdapter = new DevicePanelSettingTypeAdapter(mBigType, typeList);
            mDevicePanelSettingTypeAdapter.setOnItemClickListener(position -> {
                mDevicePanelSettingTypeAdapter.updateSelectedItem(mSelectedTab, position);
                if (getActivity() instanceof DevicePanelSettingActivity) {
                    DevicePanelSettingActivity activity = (DevicePanelSettingActivity) getActivity();
                    activity.setSportItem(activity.CATEGORY[mSelectedTab].getTypes()[position]);
                    activity.mPanelSettingTabAdapter.removeUnselectedItem();
                }
            });
        }
        return mDevicePanelSettingTypeAdapter;
    }
}